home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / cap / tools / flipobj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.1 KB  |  142 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /*
  19.  * Kurt Akeley
  20.  * 22 April 1991
  21.  *
  22.  * Hacked version of /jake/att/usr/src/cmd/demo/src/flip/flipobj.c
  23.  */
  24.  
  25. #include <math.h>
  26. #include <stdio.h>
  27. #include <gl/gl.h>
  28. #include <gl/device.h>
  29.  
  30. #include <flip.h>
  31.  
  32. flipobj
  33. *readflipobj(name)
  34. char *name;
  35. {
  36.     FILE    *inf;
  37.     flipobj    *obj;
  38.     int        i, j;
  39.     int        nlongs;
  40.     int        magic;
  41.     int        *ip;
  42.     char s[200];
  43.  
  44.     inf = fopen(name,"r");
  45.     if(!inf) {
  46.         sprintf(s,"%s%s",MODELDIR,name);
  47.         inf = fopen(s,"r");
  48.     }
  49.     if(!inf) {
  50.         fprintf(stderr,"readfast: can't open input file %s\n",name);
  51.         fprintf(stderr,"          can't open input file %s\n",s);
  52.         exit(1);
  53.     }
  54.     fread(&magic,sizeof(int),1,inf);
  55.     if(magic != FASTMAGIC) {
  56.     fprintf(stderr,"readfast: bad magic in object file\n");
  57.     fclose(inf);
  58.         exit(1);
  59.     }
  60.     obj = (flipobj *)malloc(sizeof(flipobj));
  61.     fread(&obj->npoints,sizeof(int),1,inf);
  62.     /*** IGNORE COLORS FIELD ***/
  63.     fread(&magic,sizeof(int),1,inf);
  64.  
  65.     /*
  66.      * Insure that the data is quad-word aligned and begins on a page
  67.      * boundary.  This shields us from the performance loss which occurs 
  68.      * whenever we try to fetch data which straddles a page boundary (the OS
  69.      * has to map in the next virtual page and re-start the DMA transfer).
  70.      */
  71.     nlongs = 8 * obj->npoints;
  72.     obj->data = (float *) malloc(nlongs*sizeof(int) + 4096);
  73.     obj->data = (float *) (((int)(obj->data)) + 0xfff);
  74.     obj->data = (float *) (((int)(obj->data)) & 0xfffff000);
  75.  
  76.     for (i = 0, ip = (int *)obj->data;  i < nlongs/4;  i++, ip += 4)
  77.         fread(ip, 3 * sizeof(int), 1, inf);
  78.     fclose(inf);
  79.  
  80.     return obj;
  81. }
  82.  
  83. void
  84. drawflipobj(obj)
  85. flipobj *obj;
  86. {
  87.     register float *p,*end;
  88.  
  89.     p = obj->data;
  90.     end = p + 8 * obj->npoints;
  91.  
  92.     while ( p < end) {
  93.         bgnpolygon();
  94.         n3f(p);
  95.         v3f(p+4);
  96.         n3f(p+8);
  97.         v3f(p+12);
  98.         n3f(p+16);
  99.         v3f(p+20);
  100.         n3f(p+24);
  101.         v3f(p+28);
  102.         endpolygon();
  103.         p += 32;
  104.     }
  105. }
  106.  
  107.  
  108. /*
  109.  * objmaxpoint
  110.  *
  111.  * find the vertex farthest from the origin,
  112.  * so we can set the near and far clipping planes tightly.
  113.  */
  114.  
  115. #define MAXVERT(v) if ( (len = sqrt(    (*(v))  *  (*(v))  +      \
  116.                     (*(v+1)) * (*(v+1)) +           \
  117.                     (*(v+2)) * (*(v+2)) )) > max)  \
  118.             max = len;
  119.  
  120. float
  121. objmaxpoint(obj)
  122. flipobj *obj;
  123. {
  124.     register float *p, *end;
  125.     register int npolys;
  126.     register float len;
  127.     register float max = 0.0;
  128.  
  129.     p = obj->data;
  130.  
  131.     end = p + 8 * obj->npoints;
  132.     while ( p < end) {
  133.         MAXVERT(p+4);
  134.         MAXVERT(p+12);
  135.         MAXVERT(p+20);
  136.         MAXVERT(p+28);
  137.         p += 32;
  138.     }
  139.  
  140.     return max;
  141. }
  142.